(function ($) {
  "use strict";
  /*
  * author GL
  * params = {
  *   title 内容 String
  *   text 标题 String
  *   buttonOK alert、confirm、prompt确定按钮文案(默认'确定') String
  *   buttonCancel alert、confirm、prompt取消按钮文案(默认'取消') String
  *   buttons: [{ 底部按钮个数 Array, 内容{text 按钮描述，className 添加class, onClick 按钮回调}
  *     text: "确定",
  *     className: "primary"
  *   }],
  *   autoClose 按钮是否关闭弹窗(默认 关闭true) Boolean
  *   hasClose 是否显示关闭按钮(默认 不显示false) Boolean
  * }
  * */
  var defaults;
  $.modal = function (params, onOpen) {
    params = $.extend({}, defaults, params);
    var buttons = params.buttons;
    var buttonsHtml = buttons.map(function(d, i) {
      return '<a href="javascript:;" class="gzl-dialog__btn ' + (d.className || "") + '">' + d.text + '</a>';
    }).join("");
    var tpl = '<div class="gzl-dialog"><div class="gzl-dialog__box">' +
      '<div class="gzl-dialog__con">' +
      (params.hasClose ? '<div class="gzl-dialog__close"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-question"></use></svg></div>':'') +
      (params.title ? '<div class="gzl-dialog__hd"><strong>'+ params.title +'</strong></div>':'') +
      '<div class="gzl-dialog__bd '+(params.title?'gzl-dialog__section':'')+'">'+ params.text +'</div>' +
      '</div>' +
      '<div class="gzl-dialog__ft">' + buttonsHtml + '</div></div>' +
      '</div></div>';
    var dialog = $.openModal(tpl, onOpen);
    dialog.find(".gzl-dialog__btn").each(function(i, e) {
      var el = $(e);
      el.on('click', function() {
        if(params.autoClose) $.closeModal();
        if(buttons[i].onClick) {
          buttons[i].onClick.call(dialog);
        }
      });
    });
    dialog.find(".gzl-dialog__close").on("click", function () {
      $.closeModal();
    })
  };
  $.openModal = function(tpl, onOpen){
    var dialog = $(tpl).appendTo(document.body);
    dialog.show();
    if (onOpen) {
      dialog.transitionEnd(function () {
        onOpen.call(dialog);
      });
    }
    dialog.addClass("show");
    return dialog;
  };
  $.closeModal = function(){
    $(".gzl-dialog").removeClass("show").transitionEnd(function () {
      this.remove();
    });
  };
  /*
  * alert
  * params = {
  *   text 内容 String
  *   title 标题 String
  *   onOK 确认回调 Function
  * }
  * */
  $.alert = function(text, title, onOK) {
    var config;
    if (typeof text === 'object') {
      config = text;
    } else {
      if (typeof title === 'function') {
        onOK = arguments[1];
        title = undefined;
      }
      config = {
        text: text,
        title: title,
        onOK: onOK
      }
    }
    return $.modal({
      text: config.text,
      title: config.title,
      buttons: [{
        text: defaults.buttonOK,
        className: "primary",
        onClick: config.onOK
      }]
    });
  };
  /*
  * confirm
  * params = {
  *   text 内容 String
  *   title 标题 String
  *   onOK 确认回调 Function
  *   onCancel 取消回调 Function
  * }
  * */
  $.confirm = function(text, title, onOK, onCancel) {
    var config;
    if (typeof text === 'object') {
      config = text
    } else {
      if (typeof title === 'function') {
        onCancel = arguments[2];
        onOK = arguments[1];
        title = undefined;
      }
      config = {
        text: text,
        title: title,
        onOK: onOK,
        onCancel: onCancel
      }
    }
    return $.modal({
      text: config.text,
      title: config.title,
      buttons: [
        {
          text: defaults.buttonCancel,
          className: "default",
          onClick: config.onCancel
        },
        {
          text: defaults.buttonOK,
          className: "primary",
          onClick: config.onOK
        }]
    });
  };
  /*
  * prompt
  * params = {
  *   text 内容 String
  *   title 标题 String
  *   onOK 确认回调 Function
  *   onCancel 取消回调 Function
  *   input 输入内容 String
  *   empty 能否为空(默认 必填false) Boolean
  * }
  * */
  $.prompt = function(text, title, onOK, onCancel, input) {
    var config;
    if (typeof text === 'object') {
      config = text;
    } else {
      if (typeof title === 'function') {
        input = arguments[3];
        onCancel = arguments[2];
        onOK = arguments[1];
        title = undefined;
      }
      config = {
        text: text,
        title: title,
        input: input,
        onOK: onOK,
        onCancel: onCancel,
        empty: false
      }
    }
    var modal = $.modal({
      text: '<p class="gzl-prompt-text">'+(config.text || '')+'</p><input type="text" class="gzl-input gzl-prompt-input" id="gzl-prompt-input" value="' + (config.input || '') + '" />',
      title: config.title,
      autoClose: false,
      buttons: [
        {
          text: defaults.buttonCancel,
          className: "default",
          onClick: function () {
            $.closeModal();
            config.onCancel && config.onCancel.call(modal);
          }
        },
        {
          text: defaults.buttonOK,
          className: "primary",
          onClick: function() {
            var input = $("#gzl-prompt-input").val();
            if (!config.empty && (input === "" || input === null)) {
              this.find('.gzl-prompt-input').focus()[0].select();
              return false;
            }
            $.closeModal();
            config.onOK && config.onOK.call(modal, input);
          }
        }]
    }, function () {
      this.find('.gzl-prompt-input').focus()[0].select();
    });
    return modal;
  };
  defaults = $.modal.prototype.defaults = {
    title: null,
    text: '内容',
    buttonOK: "确定",
    buttonCancel: "取消",
    buttons: [{
      text: "确定",
      className: "primary"
    }],
    autoClose: true,
    hasClose: false
  };
})($);